package com.lndsnx.quick;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @author: lndsnx
 * @date: 2025/7/15 22:53
 * @Version: 1.0
 * @description: 快速开始
 */
public class Producer {

    public static void main(String[] args) throws MQClientException, InterruptedException {

        DefaultMQProducer producer = new DefaultMQProducer("default_group");
        producer.setNamesrvAddr("10.0.0.53:9876");
        producer.start();

        for (int i = 0; i < 2; i++) {
            try {
                Message msg = new Message("TopicTest",
                        "TagA",
                        ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
                );
                msg.setKeys("testProducer" + i);

                // 延时消息，4.X 版本只能通过配置级别，不支持自定义时间
                // messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
                // 原理，系统内置18个队列 SCHEDULE_TOPIC_XXX，通过队列的延时属性，将消息放入对应的队列中，然后定时消费
                // msg.setDelayTimeLevel(3);

                // 无返回值，性能最高，发送失败不知道
                // producer.sendOneway(msg);
                // 阻塞（同步）发送，发送成功返回结果
                SendResult sendResult = producer.send(msg);

                System.out.printf("%s%n", sendResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        producer.shutdown();
    }
}
